// pages/addressMgr/addressMgr.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    centerLat: '',
    centerLng: '',
    markers: [],
    editMarker: false,
    polylineArray: []
  },
  regionchange(e) {
    console.log(e.type)
  },
  addMarker() {
    var flag = !this.data.editMarker;
    this.setData({
      editMarker: flag
    })
  },
  addMarkerLine() {
    console.info("marker line start", this.data.markers);
    var points = this.data.markers.map(item => {
      return {
        latitude: item.latitude,
        longitude: item.longitude
      }
    });
    var polyline={
      points:points,
      color: "#ff6600",
      width: 2,
      dottedLine: false,
      arrowLine: true,
      borderColor:"#000",
      borderWidth:10
    }
    var polylineArrayTemp=[polyline]
    this.setData({
      polylineArray:polylineArrayTemp
    })
  },
  bindtapHandler(e) {
    if (this.data.editMarker) {
      console.info("marker is not active!")
      return;
    }
    var markersCopy = this.data.markers;
    var markerSingle = {
      latitude: e.detail.latitude,
      longitude: e.detail.longitude,
      iconPath: ''
    }
    markersCopy.push(markerSingle);
    this.setData({
      markers: markersCopy
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.getSetting({
      success: res => {
        //当没有权限时去获取权限
        if (!res.authSetting['scope.userLocation']) {
          console.info(222);
          wx.authorize({
            scope: 'scope.userLocation',
            success(response) {
              //拿到了用户的位置权限
              this.getLocation();
            }
          })
        } else {
          //拿到了用户的位置权限
          this.getLocation();
        }
      }
    })
  },
  getLocation() {
    console.info("get location info..")
    wx.getLocation({
      type: 'wgs84',
      success: res => {
        console.info("get location address:", res);
        this.setData({
          centerLat: res.latitude,
          centerLng: res.longitude
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})